package com.health.fragment.focusgroup;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.model.PeopleType;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.BD_ORGANIZE_EMPLOYEE;
import com.health.tablebean.CD_TUBERCULOSIS_FOLLOWUP;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.viewscontrols.FWMYDPJActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.viewscontrols.focusgroup.FJHActivity;
import com.health.viewscontrols.focusgroup.GXYActivity;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 重点人群-- 肺结核---随访信息
 *
 * @author wangxiangyi
 * @date 2018/3/8
 */
public class FJH_SFXXFragment extends NovaFragment implements onFragmentSave {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private FDbManager fdbmanger;
    private ARCHIVE_BASEINFO userInfo;
    private Unbinder unbinder;
    /**
     * 保存标识 -1:未创建档案、1：保存成功
     */
    private int fromFollowup = -1;
    /**
     * 姓名
     */
    @BindView(R.id.tv_name)
    TextView tvName;
    /**
     * 编号
     */
    @BindView(R.id.tv_numbering)
    TextView tvNumbering;
    /**
     * 随访时间
     */
    @BindView(R.id.etwd_follow_date)
    EditTextWithDate etwdFollowDate;
    /**
     * 治疗月序
     */
    @BindView(R.id.et_treatment_month)
    EditText etTreatmentMonth;
    /**
     * 督导人员
     */
    @BindView(R.id.mgv_supervisor)
    MyGridView mgvSupervisor;
    /**
     * 随访方式
     */
    @BindView(R.id.mgv_follow_method)
    MyGridView mgvFollowMethod;
    /**
     * 症状及体征
     */
    @BindView(R.id.mgv_symptoms_and_signs)
    MyGridView mgvSymptomsAndSigns;
    /**
     * 吸烟（日吸烟量）
     */
    @BindView(R.id.et_day_smoking)
    EditText etDaySmoking;
    /**
     * 吸烟（目标吸烟量）
     */
    @BindView(R.id.et_smoking_aims)
    EditText etSmokingAims;
    /**
     * 饮酒（日饮酒量）
     */
    @BindView(R.id.et_day_drinking)
    EditText etDayDrinking;
    /**
     * 饮酒（目标饮酒量）
     */
    @BindView(R.id.et_drinking_aims)
    EditText etDrinkingAims;
    /**
     * 化疗方案
     */
    @BindView(R.id.et_chemotherapy)
    EditText etChemotherapy;
    /**
     * 用法
     */
    @BindView(R.id.mgv_usage)
    MyGridView mgvUsage;
    /**
     * 药品剂量
     */
    @BindView(R.id.mgv_drug_dosage)
    MyGridView mgvDrugDosage;
    /**
     * 漏服药次数
     */
    @BindView(R.id.et_lfycs)
    EditText etLfycs;
    /**
     * 药物不良反应
     */
    @BindView(R.id.mgv_medical_side_effects)
    MyGridView mgvMedicalSideEffects;
    /**
     * 并发症或合并症
     */
    @BindView(R.id.mgv_complication_or_comorbidity)
    MyGridView mgvComplicationOrComorbidity;
    /**
     * 科别
     */
    @BindView(R.id.et_bk)
    EditText etBk;
    /**
     * 原因
     */
    @BindView(R.id.et_reason)
    EditText etReason;
    /**
     * 2周内随访，随访结果
     */
    @BindView(R.id.et_two_week_follow_results)
    EditText etTwoWeekFollowResults;
    /**
     * 处理意见
     */
    @BindView(R.id.et_handling_comments)
    EditText etHandlingComments;
    /**
     * 下次随访时间
     */
    @BindView(R.id.etwd_next_visit_date)
    EditTextWithDate etwdNextVisitDate;
    /**
     * 随访医生签名
     */
    @BindView(R.id.ed_doctors_signature)
    NiceSpinner edDoctorsSignature;
    /**
     * 出现停止治疗时间
     */
    @BindView(R.id.etwd_cxtzzlsj)
    EditTextWithDate etwdCxtzzlsj;
    /**
     * 停止治疗原因
     */
    @BindView(R.id.mgv_tzzlyy)
    MyGridView mgvTzzlyy;
    /**
     * 应访视患者
     */
    @BindView(R.id.et_visits_patient)
    EditText etVisitsPatient;
    /**
     * 实际访视
     */
    @BindView(R.id.et_actual_visit)
    EditText etActualVisit;
    /**
     * 应服药
     */
    @BindView(R.id.et_medicine_frequency)
    EditText etMedicineFrequency;
    /**
     * 实际服药
     */
    @BindView(R.id.et_actual_medicine)
    EditText etActualMedicine;
    /**
     * 服药率
     */
    @BindView(R.id.et_medication_rate)
    EditText etMedicationRate;
    /**
     * 评估医生签字
     */
    @BindView(R.id.ns_pgysqz)
    NiceSpinner nsPgysqz;
    /**
     * 督导人员适配器
     */
    private RadioButtonAdapter adapterDdry;
    /**
     * 随访方式适配器
     */
    private RadioButtonAdapter adapterSffs;
    /**
     * 症状及体征适配器
     */
    private CheckBoxAdapter adapterZzjtz;
    /**
     * 用法适配器
     */
    private RadioButtonAdapter adapterYf;
    /**
     * 药品剂量适配器
     */
    private CheckBoxAdapter adapterYpjl;
    /**
     * 药物不良反应适配器
     */
    private RadioButtonAdapter adapterYwblfy;
    /**
     * 并发症或合并症适配器
     */
    private RadioButtonAdapter adapterBfzhhbz;
    /**
     * 停止治疗原因适配器
     */
    private CheckBoxAdapter adapterTzzlyy;

    private CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup;
    private List szfs_list = new ArrayList<>();


    @SuppressLint("InflateParams")
    @Override
    public View findViews(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fjh_sfxx, null);
    }


    @Override
    public void setupView(View view) {
        unbinder = ButterKnife.bind(this, view);
        fdbmanger = NovaApplication.fdbmanger;
        //获取用户信息
        userInfo = NovaApplication.getUserInfo();
        etwdFollowDate.setBefore(true);
    }


    @Override
    public void addListener() {
    }


    @SuppressLint("SetTextI18n")
    @Override
    public void setData() {
        //获取医生人员
        GlobalConts.doctorList(edDoctorsSignature);
        GlobalConts.doctorList(nsPgysqz);

        tvName.setText("姓名：" + userInfo.getCUSTOMERNAME());
        if (TextUtils.isEmpty(userInfo.getCUSTOMERID())){
            tvNumbering.setText("编号：");
        }else {
            tvNumbering.setText("编号：" + userInfo.getCUSTOMERID());
        }
        bindData(mgvSupervisor, "督导人员", "DM102-07");
        bindData(mgvFollowMethod, "随访方式", "DM04-15");
        bindData(mgvSymptomsAndSigns, "症状及体征", "DM102-04");
        bindData(mgvUsage, "用法", "DM102-05");
        bindData(mgvDrugDosage, "药品剂量", "DM102-06");
        bindData(mgvMedicalSideEffects, "药物不良反应", "DM01-11");
        bindData(mgvComplicationOrComorbidity, "并发症或合并症", "DM01-11");
        bindData(mgvTzzlyy, "停止治疗原因", "DM102-09");
        obtainTuberculosisFollowupInfo();
    }

    /**
     * 查询数据库数据
     *
     * @param myGridView 标题
     * @param title      标题
     * @param where      查询条件
     * @date 2018/03/03
     */
    private void bindData(MyGridView myGridView, String title, String where) {
        //根据parentid字段获取bd_dd_info数据库的信息
        List<BD_DD_INFO> listZZs = fdbmanger.findAllByWhereInInfoDB(BD_DD_INFO.class, "PARENTID='" + where + "'");
        if (listZZs.size() > 0) {
            //按条件把适配器添加到组建中
            switch (title) {
                case "督导人员":
                    adapterDdry = new RadioButtonAdapter(listZZs, getActivity());
                    adapterDdry.setOtherTextVisible(false);
                    myGridView.setAdapter(adapterDdry);
                    break;
                case "随访方式":
                    szfs_list = listZZs;
                    adapterSffs = new RadioButtonAdapter(listZZs, getActivity());
                    myGridView.setAdapter(adapterSffs);
                    break;
                case "症状及体征":
                    adapterZzjtz = new CheckBoxAdapter(getActivity(), listZZs);
                    myGridView.setAdapter(adapterZzjtz);
                    break;
                case "用法":
                    adapterYf = new RadioButtonAdapter(listZZs, getActivity());
                    myGridView.setAdapter(adapterYf);
                    break;
                case "药品剂量":
                    adapterYpjl = new CheckBoxAdapter(getActivity(), listZZs);
                    myGridView.setAdapter(adapterYpjl);
                    break;
                case "药物不良反应":
                    adapterYwblfy = new RadioButtonAdapter(listZZs, getActivity());
                    adapterYwblfy.setOtherMaxLength(150);
                    myGridView.setAdapter(adapterYwblfy);
                    break;
                case "并发症或合并症":
                    adapterBfzhhbz = new RadioButtonAdapter(listZZs, getActivity());
                    adapterBfzhhbz.setOtherMaxLength(150);
                    myGridView.setAdapter(adapterBfzhhbz);
                    break;
                case "停止治疗原因":
                    adapterTzzlyy = new CheckBoxAdapter(getActivity(), listZZs);
                    myGridView.setAdapter(adapterTzzlyy);
                    break;
                default:
            }
        }
    }

    @Override
    public void save() {
        if (TextUtils.isEmpty(etwdFollowDate.getText().toString())) {
            etwdFollowDate.setBackgroundColor(Color.YELLOW);
            ((FJHActivity) getActivity()).showTextDialog("请选择随访时间");
            return;
        } else {
            etwdFollowDate.setBackgroundColor(Color.WHITE);
        }
        if (TextUtils.isEmpty(etwdNextVisitDate.getText().toString())) {
            etwdNextVisitDate.setBackgroundColor(Color.YELLOW);
            ((FJHActivity) getActivity()).showTextDialog("请选择下次随访时间");
            return;
        } else {
            etwdNextVisitDate.setBackgroundColor(Color.WHITE);
        }
        if (TextUtils.isEmpty(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getUSER_ID())) {
            edDoctorsSignature.setBackgroundColor(Color.YELLOW);
            ((FJHActivity) getActivity()).showTextDialog("请选择随访医生");
            return;
        } else {
            edDoctorsSignature.setBackgroundColor(Color.WHITE);
        }
        if (TextUtils.isEmpty(NovaApplication.getDoctorList().get(nsPgysqz.getSelectedIndex()).getUSER_ID())) {
            nsPgysqz.setBackgroundColor(Color.YELLOW);
            ((FJHActivity) getActivity()).showTextDialog("请选择评估医生");
            return;
        } else {
            nsPgysqz.setBackgroundColor(Color.WHITE);
        }
//        if (TextUtils.isEmpty(etTreatmentMonth.getText().toString())) {
//            etTreatmentMonth.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入治疗月序");
//            return;
//        } else {
//            etTreatmentMonth.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterDdry.getChecked())) {
//            mgvSupervisor.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择督导人员");
//            return;
//        } else {
//            mgvSupervisor.setBackgroundColor(Color.WHITE);
//        }
        if (TextUtils.isEmpty(adapterSffs.getChecked())) {
            mgvFollowMethod.setBackgroundColor(Color.YELLOW);
            ((FJHActivity) getActivity()).showTextDialog("请选择随访方式");
            return;
        } else {
            mgvFollowMethod.setBackgroundColor(Color.WHITE);
        }
//        if (TextUtils.isEmpty(adapterZzjtz.getCheckedIdsStr())) {
//            mgvSymptomsAndSigns.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择症状及体征");
//            return;
//        } else {
//            mgvSymptomsAndSigns.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etChemotherapy.getText().toString())) {
//            etChemotherapy.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入化疗方案");
//            return;
//        } else {
//            etChemotherapy.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterYf.getChecked())) {
//            mgvUsage.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择用法");
//            return;
//        } else {
//            mgvUsage.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterYpjl.getCheckedIdsStr())) {
//            mgvDrugDosage.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择药品剂量");
//            return;
//        } else {
//            mgvDrugDosage.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etLfycs.getText().toString())) {
//            etLfycs.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入漏服药品次数");
//            return;
//        } else {
//            etLfycs.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterYwblfy.getChecked())) {
//            mgvMedicalSideEffects.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择药物不良反应");
//            return;
//        } else {
//            mgvMedicalSideEffects.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterBfzhhbz.getChecked())) {
//            mgvComplicationOrComorbidity.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择并发症或合并症");
//            return;
//        } else {
//            mgvComplicationOrComorbidity.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etBk.getText().toString())) {
//            etBk.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入科别");
//            return;
//        } else {
//            etBk.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etReason.getText().toString())) {
//            etReason.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入原因");
//            return;
//        } else {
//            etReason.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etTwoWeekFollowResults.getText().toString())) {
//            etTwoWeekFollowResults.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入2周内随访，随访结果");
//            return;
//        } else {
//            etTwoWeekFollowResults.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etHandlingComments.getText().toString())) {
//            etHandlingComments.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入处理意见");
//            return;
//        } else {
//            etHandlingComments.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etwdCxtzzlsj.getText().toString())) {
//            etwdCxtzzlsj.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择出现停止治疗时间");
//            return;
//        } else {
//            etwdCxtzzlsj.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(adapterTzzlyy.getCheckedIdsStr())) {
//            mgvTzzlyy.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请选择停止治疗原因");
//            return;
//        } else {
//            mgvTzzlyy.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etVisitsPatient.getText().toString())) {
//            etVisitsPatient.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入应访视患者");
//            return;
//        } else {
//            etVisitsPatient.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etActualVisit.getText().toString())) {
//            etActualVisit.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入实际访视");
//            return;
//        } else {
//            etActualVisit.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etMedicineFrequency.getText().toString())) {
//            etMedicineFrequency.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入应服药");
//            return;
//        } else {
//            etMedicineFrequency.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etActualMedicine.getText().toString())) {
//            etActualMedicine.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入实际服药");
//            return;
//        } else {
//            etActualMedicine.setBackgroundColor(Color.WHITE);
//        }
//        if (TextUtils.isEmpty(etMedicationRate.getText().toString())) {
//            etMedicationRate.setBackgroundColor(Color.YELLOW);
//            ((FJHActivity) getActivity()).showTextDialog("请输入服药率");
//            return;
//        } else {
//            etMedicationRate.setBackgroundColor(Color.WHITE);
//        }
        cdTuberculosisFollowup = new CD_TUBERCULOSIS_FOLLOWUP();
        cdTuberculosisFollowup.setIS_UPDATE("Y");
        saveBaseInfo();
        saveLifestyleGuide();
        saveMedication();
        saveReferral();
        saveStopTreatmentAndReason();
        saveWholeSystemSituation();
        saveCreateAndUpdateInfo();
    }

    /**
     * 保存基本信息
     */
    private void saveBaseInfo() {
        //档案编号
        String s = "：";
        if (tvNumbering.getText().toString().split(s).length >= 2) {
            cdTuberculosisFollowup.setPERSONALID(tvNumbering.getText().toString().split(s)[1]);
        }
        cdTuberculosisFollowup.setCARDID(NovaApplication.getUserInfo().getIDCARD());
        try {
            cdTuberculosisFollowup.setHAPPENTIME(format.parse(etwdFollowDate.getText().toString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cdTuberculosisFollowup.setTREAT_MONTH(etTreatmentMonth.getText().toString());
        cdTuberculosisFollowup.setSUPERVISOR(adapterDdry.getChecked());
        cdTuberculosisFollowup.setSUPERVISOR(adapterDdry.getChecked());
        cdTuberculosisFollowup.setVISIT_WAY(adapterSffs.getChecked());
        cdTuberculosisFollowup.setSYMPTOM(getcheckedbdDdInfo(adapterZzjtz));
    }

    /**
     * 保存生活方式指导
     */
    private void saveLifestyleGuide() {
        cdTuberculosisFollowup.setSMOKE(etDaySmoking.getText().toString());
        cdTuberculosisFollowup.setTARGET_SMOKE(etSmokingAims.getText().toString());
        cdTuberculosisFollowup.setDRINK(etDayDrinking.getText().toString());
        cdTuberculosisFollowup.setTARGET_DRINK(etDrinkingAims.getText().toString());
    }

    /**
     * 保存用药
     */
    private void saveMedication() {
        cdTuberculosisFollowup.setCHEMOTHERAPY(etChemotherapy.getText().toString());
        cdTuberculosisFollowup.setDRUG_USEAGE(adapterYf.getChecked());
        cdTuberculosisFollowup.setDRUG_TYPE(getcheckedbdDdInfo(adapterYpjl));
        cdTuberculosisFollowup.setDRUG_TIME(etLfycs.getText().toString());
        /*药物不良反应*/
        cdTuberculosisFollowup.setDRUG_EFFECTS(adapterYwblfy.getChecked());
        /*药物不良反应描述*/
        cdTuberculosisFollowup.setEFFECTS_STATE(getDescription(adapterYwblfy));
        /*并发症或合并症*/
        cdTuberculosisFollowup.setCOMPLICATION(adapterBfzhhbz.getChecked());
        /*并发症或合并症描述*/
        cdTuberculosisFollowup.setCOMPLICATION_STATE(getDescription(adapterBfzhhbz));
    }

    /**
     * 保存转诊
     */
    private void saveReferral() {
        cdTuberculosisFollowup.setTRANSFER_REGION(etBk.getText().toString());
        cdTuberculosisFollowup.setTRANSFER_REASON(etReason.getText().toString());
        cdTuberculosisFollowup.setTRANSFER_RESULT(etTwoWeekFollowResults.getText().toString());
        cdTuberculosisFollowup.setADVICE(etHandlingComments.getText().toString());
        cdTuberculosisFollowup.setNEXT_VISIT_DATE(etwdNextVisitDate.getDate());
        cdTuberculosisFollowup.setINPUT_MAN(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getUSER_ID());
    }

    /**
     * 保存停止治疗及原因
     */
    private void saveStopTreatmentAndReason() {
        cdTuberculosisFollowup.setSTOP_TIME(etwdCxtzzlsj.getDate());
        cdTuberculosisFollowup.setSTOP_REASON(getcheckedbdDdInfo(adapterTzzlyy));
    }

    /**
     * 保存全程系统情况
     */
    private void saveWholeSystemSituation() {
        cdTuberculosisFollowup.setVIEW_NUMBER(etVisitsPatient.getText().toString());
        cdTuberculosisFollowup.setVIEW_TRUE_NUMBER(etActualVisit.getText().toString());
        cdTuberculosisFollowup.setDRUG_NUMBER(etMedicineFrequency.getText().toString());
        cdTuberculosisFollowup.setDRUG_TRUE_NUMBER(etActualMedicine.getText().toString());
        cdTuberculosisFollowup.setDRUG_RATE(etMedicationRate.getText().toString());
        cdTuberculosisFollowup.setDOCTOR_SIGN(NovaApplication.getDoctorList().get(nsPgysqz.getSelectedIndex()).getUSER_ID());
    }

    /**
     * 保存更新或创建档案信息
     */
    private void saveCreateAndUpdateInfo() {
        switch (fromFollowup) {
            case -1:
                cdTuberculosisFollowup.setCREATER(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getUSER_ID());
                cdTuberculosisFollowup.setCREATEORG(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getORGANCODE());
                cdTuberculosisFollowup.setCREATETIME(new Date());
                fdbmanger.save(cdTuberculosisFollowup);
                fromFollowup = 1;
                if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
                    new AlertDialog.Builder(getActivity()).setTitle("保存成功").setMessage("进行满意度评价")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(getContext(), FWMYDPJActivity.class);
                                    intent.putExtra("type", PeopleType.FJH);
                                    intent.putExtra("szfs", adapterSffs.getChecked());
                                    startActivity(intent);
                                }
                            }).show();
                } else if (GlobalConts.TYPE_GJJBGGWS.equals(NovaApplication.getType())) {
                    ((FJHActivity) getActivity()).showTextDialog("保存成功");
                }
                break;
            case 1:
                cdTuberculosisFollowup.setMODIFIED(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getUSER_ID());
                cdTuberculosisFollowup.setUPDATEORG(NovaApplication.getDoctorList().get(edDoctorsSignature.getSelectedIndex()).getORGANCODE());
                cdTuberculosisFollowup.setUPDATETIME(new Date());
                fdbmanger.updata(cdTuberculosisFollowup, "CARDID = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
                ((FJHActivity) getActivity()).showTextDialog("修改成功");
                break;
            default:
        }
    }

    /**
     * 获取肺结核随访信息
     */
    private void obtainTuberculosisFollowupInfo() {
        //获取保存数据
        List<CD_TUBERCULOSIS_FOLLOWUP> allByWhere = fdbmanger.findAllByWhere(CD_TUBERCULOSIS_FOLLOWUP.class, "CARDID = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
        if (allByWhere.size() > 0) {
            fromFollowup = 1;
            setFollowInfo(allByWhere);
        } else {
            etwdFollowDate.setDate(new Date());
            fromFollowup = -1;
        }
    }

    /**
     * 设置随访信息
     *
     * @param allByWhere 信息列表
     */
    private void setFollowInfo(List<CD_TUBERCULOSIS_FOLLOWUP> allByWhere) {
        setBaseInfo(allByWhere.get(0));
        setLifestyleGuide(allByWhere.get(0));
        setMedication(allByWhere.get(0));
        setReferral(allByWhere.get(0));
        setStopTreatmentAndReason(allByWhere.get(0));
        setWholeSystemSituation(allByWhere.get(0));
    }

    /**
     * 设置基本信息
     */
    @SuppressLint("SetTextI18n")
    private void setBaseInfo(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        tvNumbering.setText("编号：" + (!"null".equals(cdTuberculosisFollowup.getPERSONALID()) ? cdTuberculosisFollowup.getPERSONALID() : ""));
        etwdFollowDate.setDate(cdTuberculosisFollowup.getHAPPENTIME());
        etTreatmentMonth.setText(cdTuberculosisFollowup.getTREAT_MONTH());
        if (null != cdTuberculosisFollowup.getSUPERVISOR()
                && !TextUtils.isEmpty(cdTuberculosisFollowup.getSUPERVISOR())) {
            adapterDdry.setChecked(cdTuberculosisFollowup.getSUPERVISOR());
        }
        if (null != cdTuberculosisFollowup.getVISIT_WAY()
                && !TextUtils.isEmpty(cdTuberculosisFollowup.getVISIT_WAY())) {
            adapterSffs.setChecked(cdTuberculosisFollowup.getVISIT_WAY());
        }
        if (null != cdTuberculosisFollowup.getSYMPTOM()
                && !TextUtils.isEmpty(cdTuberculosisFollowup.getSYMPTOM())) {
            adapterZzjtz.setCheckedCodeList(splitString(cdTuberculosisFollowup.getSYMPTOM()));
        }
    }

    /**
     * 设置生活方式指导
     */
    private void setLifestyleGuide(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        etDaySmoking.setText(cdTuberculosisFollowup.getSMOKE());
        etSmokingAims.setText(cdTuberculosisFollowup.getTARGET_SMOKE());
        etDayDrinking.setText(cdTuberculosisFollowup.getDRINK());
        etDrinkingAims.setText(cdTuberculosisFollowup.getTARGET_DRINK());
    }

    /**
     * 设置用药
     */
    private void setMedication(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        etChemotherapy.setText(cdTuberculosisFollowup.getCHEMOTHERAPY());
        adapterYf.setChecked(cdTuberculosisFollowup.getDRUG_USEAGE());
        adapterYpjl.setCheckedCodeList(splitString(cdTuberculosisFollowup.getDRUG_TYPE()));
        etLfycs.setText(cdTuberculosisFollowup.getDRUG_TIME());
        /*药物不良反应*/
        adapterYwblfy.setChecked(cdTuberculosisFollowup.getDRUG_EFFECTS(), cdTuberculosisFollowup.getEFFECTS_STATE());
        /*并发症或合并症*/
        adapterBfzhhbz.setChecked(cdTuberculosisFollowup.getCOMPLICATION(), cdTuberculosisFollowup.getCOMPLICATION_STATE());
    }

    /**
     * 设置转诊
     */
    private void setReferral(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        etBk.setText(cdTuberculosisFollowup.getTRANSFER_REGION());
        etReason.setText(cdTuberculosisFollowup.getTRANSFER_REASON());
        etTwoWeekFollowResults.setText(cdTuberculosisFollowup.getTRANSFER_RESULT());
        etHandlingComments.setText(cdTuberculosisFollowup.getADVICE());
        etwdNextVisitDate.setDate(cdTuberculosisFollowup.getNEXT_VISIT_DATE());
        /* 随访医生签名*/
        List<BD_ORGANIZE_EMPLOYEE> doctorList = NovaApplication.getDoctorList();
        for (int item = 0; item < doctorList.size(); item++) {
            if (doctorList.get(item).getUSER_ID().equals(cdTuberculosisFollowup.getINPUT_MAN())) {
                edDoctorsSignature.setSelectedIndex(item);
            }
        }
    }

    /**
     * 设置停止治疗及原因
     */
    private void setStopTreatmentAndReason(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        etwdCxtzzlsj.setDate(cdTuberculosisFollowup.getSTOP_TIME());
        adapterTzzlyy.setCheckedCodeList(splitString(cdTuberculosisFollowup.getSTOP_REASON()));
    }

    /**
     * 设置全程系统情况
     */
    private void setWholeSystemSituation(CD_TUBERCULOSIS_FOLLOWUP cdTuberculosisFollowup) {
        etVisitsPatient.setText(cdTuberculosisFollowup.getVIEW_NUMBER());
        etActualVisit.setText(cdTuberculosisFollowup.getVIEW_TRUE_NUMBER());
        etMedicineFrequency.setText(cdTuberculosisFollowup.getDRUG_NUMBER());
        etActualMedicine.setText(cdTuberculosisFollowup.getDRUG_TRUE_NUMBER());
        etMedicationRate.setText(cdTuberculosisFollowup.getDRUG_RATE());

        /* 评估医生签名*/
        List<BD_ORGANIZE_EMPLOYEE> doctorList = NovaApplication.getDoctorList();
        for (int item = 0; item < doctorList.size(); item++) {
            if (doctorList.get(item).getUSER_ID().equals(cdTuberculosisFollowup.getDOCTOR_SIGN())) {
                nsPgysqz.setSelectedIndex(item);
            }
        }
    }

    /**
     * 获取多选框的值
     *
     * @param mAdapter 适配器
     * @return 选择的值
     */
    private String getcheckedbdDdInfo(CheckBoxAdapter mAdapter) {
        if (null != mAdapter) {
            ArrayList<BD_DD_INFO> checkedbdDdInfo = mAdapter.getCheckedBD_DD_INFO();
            if (checkedbdDdInfo.size() > 0) {
                StringBuilder stringBuffer = new StringBuilder();
                for (BD_DD_INFO item : checkedbdDdInfo) {
                    stringBuffer.append(item.getID());
                    stringBuffer.append(",");
                }
                stringBuffer.deleteCharAt(stringBuffer.length() - 1);
                return stringBuffer.toString();
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * 获取描述信息
     *
     * @param mAdapter 获取值的适配器
     * @return 描述信息
     */
    private String getDescription(RadioButtonAdapter mAdapter) {
        String description = null;
        switch (mAdapter.getChecked()) {
            /*督导人员*/
            /*医生*/
            case "DM102-07_1":
                /*家属*/
            case "DM102-07_2":
                /*自服药*/
            case "DM102-07_3":
                /*药物不良反应、并发症或合并症：无*/
            case "DM01-11_1":
                description = null;
                break;
            /*其他*/
            case "DM102-07_4":
                /*药物不良反应、并发症或合并症：有*/
            case "DM01-11_2":
                description = mAdapter.getCheckedOther();
                break;
            default:
        }
        return description;
    }

    /**
     * 拆分字符串
     *
     * @param key 数据
     */
    private List<String> splitString(String key) {
        return new ArrayList<>(Arrays.asList(key.split(",")));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        assert rootView != null;
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}